﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace HomeStayWebCP.Controllers.Admin
{
    public class GenericReportController : Controller
    {
        //
        // GET: /GenericReport/

        public ActionResult Index()
        {
            return View();
        }
        #region Hoangdx Report Get Top Post Users
        public void ShowGenericTopPostUserRpt()
        {
            try
            {
                bool isValid = true;

                string strReportName = System.Web.HttpContext.Current.Session["Rpt_TopPostUserName"].ToString();    // Setting ReportName 
                var rptSource = System.Web.HttpContext.Current.Session["Rpt_TopPostUserSource"];
                string strFromDate = System.Web.HttpContext.Current.Session["rptTopPostUserFromDate"].ToString();     // Setting FromDate 
                string strToDate = System.Web.HttpContext.Current.Session["rptTopPostUserToDate"].ToString();         // Setting ToDate  

                if (string.IsNullOrEmpty(strReportName))
                {
                    isValid = false;
                }

                if (isValid)
                {
                    ReportDocument rd = new ReportDocument();
                    string strRptPath = System.Web.HttpContext.Current.Server.MapPath("~/Report/") + "CrystalReport//" + strReportName;
                    rd.Load(strRptPath);
                    if (rptSource != null && rptSource.GetType().ToString() != "System.String")
                        rd.SetDataSource(rptSource);

                    if (!string.IsNullOrEmpty(strFromDate))
                        rd.SetParameterValue("fromdate", strFromDate);

                    if (!string.IsNullOrEmpty(strToDate))
                        rd.SetParameterValue("todate", strToDate);

                    rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");


                    // Clear all sessions value
                    Session["Rpt_TopPostUserName"] = null;
                    Session["Rpt_TopPostUserSource"] = null;
                }
                else
                {
                    Response.Write("<H2>Nothing Found; No Report name found</H2>");
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
        #endregion

        #region Hoangdx Report Get Top Region
        public void ShowGenericTopRegionRpt()
        {
            try
            {
                bool isValid = true;

                string strReportName = System.Web.HttpContext.Current.Session["Rpt_TopRegionName"].ToString();    // Setting ReportName 
                var rptSource = System.Web.HttpContext.Current.Session["Rpt_TopRegionSource"];
                string strFromDate = System.Web.HttpContext.Current.Session["rptTopRegionFromDate"].ToString();     // Setting FromDate 
                string strToDate = System.Web.HttpContext.Current.Session["rptTopRegionToDate"].ToString();         // Setting ToDate  

                if (string.IsNullOrEmpty(strReportName))
                {
                    isValid = false;
                }

                if (isValid)
                {
                    ReportDocument rd = new ReportDocument();
                    string strRptPath = System.Web.HttpContext.Current.Server.MapPath("~/Report/") + "CrystalReport//" + strReportName;
                    rd.Load(strRptPath);
                    if (rptSource != null && rptSource.GetType().ToString() != "System.String")
                        rd.SetDataSource(rptSource);

                    if (!string.IsNullOrEmpty(strFromDate))
                        rd.SetParameterValue("fromdate", strFromDate);

                    if (!string.IsNullOrEmpty(strToDate))
                        rd.SetParameterValue("todate", strToDate);
                    rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");


                    // Clear all sessions value
                    Session["Rpt_TopRegionName"] = null;
                    Session["Rpt_TopRegionSource"] = null;
                }
                else
                {
                    Response.Write("<H2>Nothing Found; No Report name found</H2>");
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
        #endregion

        #region Hoangdx Report Get TopUserInMonths
        public void ShowGenericTopUserInMonthsRpt()
        {
            try
            {
                bool isValid = true;

                string strReportName = System.Web.HttpContext.Current.Session["Rpt_TopUserInMonthsName"].ToString();    // Setting ReportName 
                var rptSource = System.Web.HttpContext.Current.Session["Rpt_TopUserInMonthsSource"];
                string strFromYear = System.Web.HttpContext.Current.Session["rptUserInMonthsFromYear"].ToString();     // Setting FromDate 

                if (string.IsNullOrEmpty(strReportName))
                {
                    isValid = false;
                }

                if (isValid)
                {
                    ReportDocument rd = new ReportDocument();
                    string strRptPath = System.Web.HttpContext.Current.Server.MapPath("~/Report/") + "CrystalReport//" + strReportName;
                    //string strRptPath = "http://192.168.1.151/Report/CrystalReport/TopUserInMonths.rpt";
                    rd.Load(strRptPath);
                    if (rptSource != null && rptSource.GetType().ToString() != "System.String")
                        rd.SetDataSource(rptSource);
                    if (!string.IsNullOrEmpty(strFromYear))
                        rd.SetParameterValue("fromYear", strFromYear);

                    rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");


                    // Clear all sessions value
                    Session["Rpt_TopUserInMonthsName"] = null;
                    Session["Rpt_TopUserInMonthsSource"] = null;
                }
                else
                {
                    Response.Write("<H2>Nothing Found; No Report name found</H2>");
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
        #endregion
    }
}
